import 'package:flutter/material.dart';
import 'dart:ui';

class FrostedImage extends StatelessWidget {
  final String imagePath;
  final Widget child;
  final bool isNetworkImage;
  final double blurIntensity;
  final double opacity;
  final Map<String, String>? headers;

  const FrostedImage({
    Key? key,
    required this.imagePath,
    required this.child,
    this.isNetworkImage = true,
    this.blurIntensity = 10.0,
    this.opacity = 0.2,
    this.headers,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        // 模糊背景
        Positioned.fill(
          child: _buildImageWithBlur(),
        ),

        // 前景内容
        child,
      ],
    );
  }

  Widget _buildImageWithBlur() {
    return ClipRect(
      child: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: isNetworkImage
                ? NetworkImage(imagePath, headers: headers) as ImageProvider
                : AssetImage(imagePath),
            fit: BoxFit.cover, // 裁剪以适应容器
            alignment: Alignment.center, // 居中对齐，显示中间部分
          ),
        ),
        child: BackdropFilter(
          filter:
              ImageFilter.blur(sigmaX: blurIntensity, sigmaY: blurIntensity),
          child: Container(
            color: Colors.black.withOpacity(opacity),
          ),
        ),
      ),
    );
  }
}
